<?php


class User_Model extends Model
{
    
    
    public function __construct()
    {
        parent::__construct();
    }
    
    
    /**
    * Get user list
    * 
    */
    public function userList()
    {
        $stm = $this->db->prepare('select id, login, role from users');
        $stm->execute();
        return $stm->fetchAll();
    }
    
    
    public function listUserById($id = null)
    {
        if(is_null($id)) return false;
        
        $stm = $this->db->prepare('select id, login, role from users where id = :id');
        $stm->bindValue( ":id", $id, PDO::PARAM_INT );
        $stm->execute();
        return $stm->fetchAll();   
    }
    
    /**
    *  create user
    */
    public function create($data = array())
    {
        if(count($data) <= 0) return false;
        
        $this->db->insert('users', array(
            'login' => $data['login'],
            'password' => Hash::create('md5', $data['password'], HASH_PASSWORD_KEY);
            'role' => $data['role']
        )); 
    }
    
    
    
    /**
    *  edit save user
    */
    public function editSave($data = array())
    {
        
        if(count($data) <= 0) return false;
        
        $postData = array(
            'login' => $data['login'],
            'password' => Hash::create('md5', $data['login'], HASH_PASSWORD_KEY),
            'role' => $data['role']
        );
        
        $this->db->update('users', $postData, "`id` = {$data['id']}");    
    }
    
    
    /**
    *  delete user
    */
    public function delete($id = null)
    {
        if(is_null($id)) return false;
        
        $stm = $this->db->prepare('delete from users where id = :id');
        $stm->bindValue( ":id", $id, PDO::PARAM_INT );
        $stm->execute();
        
         
    }
    
    
}